package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.LaboratoryAssistantsMapper;
import com.zhijian.medical.entity.po.LaboratoryAssistants;
import com.zhijian.medical.entity.vo.request.LaboratoryAssistantsSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.LaboratoryAssistantsSearchVo;
import com.zhijian.medical.entity.vo.response.LaboratoryAssistantsVo;
import com.zhijian.medical.service.LaboratoryAssistantsService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 在岗实验辅助人员评价业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-15 10:47:41
 */
@Slf4j
@Service
public class LaboratoryAssistantsServiceImpl implements LaboratoryAssistantsService {

	@Autowired
    private LaboratoryAssistantsMapper mapper;

    @Override
    public LaboratoryAssistants selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public LaboratoryAssistantsVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(LaboratoryAssistants laboratoryAssistants) {
        Date now = new Date();
        laboratoryAssistants.setCreateTime(now);
        laboratoryAssistants.setUpdateTime(now);
        return mapper.insert(laboratoryAssistants) > 0 ? laboratoryAssistants.getId() : 0L;
    }

    @Override
    public Long insertSelective(LaboratoryAssistants laboratoryAssistants) {
        Date now = new Date();
        laboratoryAssistants.setCreateTime(now);
        laboratoryAssistants.setUpdateTime(now);
        return mapper.insertSelective(laboratoryAssistants) > 0 ? laboratoryAssistants.getId() : 0L;
    }

    @Override
    public boolean update(LaboratoryAssistants laboratoryAssistants) {
        Date now = new Date();
        laboratoryAssistants.setUpdateTime(now);
        return mapper.updateByPrimaryKey(laboratoryAssistants) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(LaboratoryAssistants record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(LaboratoryAssistantsSaveOrUpdateVo saveOrUpdateVo) {
        LaboratoryAssistants laboratoryAssistants = new LaboratoryAssistants();
        BeanUtils.copyProperties(saveOrUpdateVo, laboratoryAssistants);
        return this.insert(laboratoryAssistants) != 0;
    }

    @Override
    public boolean modify(LaboratoryAssistantsSaveOrUpdateVo saveOrUpdateVo) {
        LaboratoryAssistants old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        LaboratoryAssistants laboratoryAssistants = new LaboratoryAssistants();
        BeanUtils.copyProperties(saveOrUpdateVo, laboratoryAssistants);
        return this.update(laboratoryAssistants);
    }

    @Override
    public LaboratoryAssistantsVo selectOne(LaboratoryAssistantsSearchVo laboratoryAssistantsSearchVo) {
        return mapper.selectOne(laboratoryAssistantsSearchVo);
     }

    @Override
    public Integer count(LaboratoryAssistantsSearchVo laboratoryAssistantsSearchVo) {
        return mapper.count(laboratoryAssistantsSearchVo);
    }

    @Override
    public List<LaboratoryAssistantsVo> selectList(LaboratoryAssistantsSearchVo laboratoryAssistantsSearchVo) {
        return mapper.selectList(laboratoryAssistantsSearchVo);
    }

    @Override
    public Page<LaboratoryAssistantsVo> selectPage(LaboratoryAssistantsSearchVo laboratoryAssistantsSearchVo) {
        AssertUtil.check(Objects.isNull(laboratoryAssistantsSearchVo.getPageNum()) || Objects.isNull(laboratoryAssistantsSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(laboratoryAssistantsSearchVo);
        //分页
        Page<LaboratoryAssistantsVo> page = new Page<>(true, count, laboratoryAssistantsSearchVo.getPageNum(), laboratoryAssistantsSearchVo.getPageSize());
        laboratoryAssistantsSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<LaboratoryAssistantsVo> responseVos = this.selectList(laboratoryAssistantsSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<LaboratoryAssistants> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(LaboratoryAssistantsSearchVo laboratoryAssistantsSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(laboratoryAssistantsSearchVo);
    }
    
}